﻿// Decompiled with JetBrains decompiler
// Type: DnProcessSimulateCommands.CApCableGenerateButtonEnabler
// Assembly: DnProcessSimulateCommands, Version=2402.0.0.890, Culture=neutral, PublicKeyToken=null
// MVID: A885D881-AEDF-447E-852A-6F13102E089C
// Assembly location: C:\Program Files\Tecnomatix_2402\eMPower\DotNetCommands\DnProcessSimulateCommands.dll

using System;
using System.Collections;
using System.ComponentModel;
using Tecnomatix.Engineering.PrivateImplementationDetails;


namespace DnProcessSimulateCommands;

public class CApCableGenerateButtonEnabler : IDisposable
{
  private bool m_hasCableFrameAttributeChangedSinceLastCableUpdate;
  private bool m_hasPassNearFrameAttributeChangedSinceLastCableUpdate;
  private bool m_hasPointCloudAttributeChangedSinceLastCableUpdate;
  private ITxCableOptimizationData m_lastOptimizationData;
  private readonly ICApFLEXCableAttributeViewModel m_attributeViewModel;
  private readonly ICApFlexCableController m_cableController;

  public CApCableGenerateButtonEnabler(
    ICApFLEXCableAttributeViewModel attributeViewModel,
    ICApFlexCableController cableController)
  {
    this.m_attributeViewModel = attributeViewModel ?? throw new ArgumentNullException(nameof (attributeViewModel));
    this.m_cableController = cableController ?? throw new ArgumentNullException(nameof (cableController));
    this.ResetObjectAttributeChangedTriggers();
    this.m_attributeViewModel.PropertyChanging += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanging);
    this.m_attributeViewModel.PropertyChanged += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
    this.m_attributeViewModel.PropertyAttributeChanged += new PropertyChangedEventHandler(this.AttributeViewModel_PropertyAttributeChanged);
  }

  public void Dispose()
  {
    this.m_attributeViewModel.PropertyChanging -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanging);
    this.m_attributeViewModel.PropertyChanged -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyChanged);
    this.m_attributeViewModel.PropertyAttributeChanged -= new PropertyChangedEventHandler(this.AttributeViewModel_PropertyAttributeChanged);
  }

  private void UpdateGenerateButtonStatus()
  {
    bool flag;
    switch (this.m_attributeViewModel.CreationMode)
    {
      case CUiFLEXCreationMode.MANUAL:
        flag = this.ShouldEnableGenerateButtonForParameterCreationMode();
        break;
      case CUiFLEXCreationMode.FROM_POINT_CLOUD:
        flag = this.ShouldEnableGenerateButtonForPointCloudCreationMode();
        break;
      default:
        throw new NotImplementedException();
    }
    if (flag == this.m_attributeViewModel.IsGenerateEnabled)
      return;
    this.m_attributeViewModel.IsGenerateEnabled = flag;
  }

  private bool ShouldEnableGenerateButtonForParameterCreationMode()
  {
    return this.IsStatusBarMessageValid() && this.m_attributeViewModel.StartFrame != null && this.m_attributeViewModel.EndFrame != null && (this.m_hasCableFrameAttributeChangedSinceLastCableUpdate || this.m_hasPassNearFrameAttributeChangedSinceLastCableUpdate || this.m_attributeViewModel.HasParameterChanged()) && this.m_cableController.CanCreateOrUpdateCable();
  }

  private bool ShouldEnableGenerateButtonForPointCloudCreationMode()
  {
    if (!this.IsStatusBarMessageValid() || this.m_attributeViewModel.StartFrame == null || this.m_attributeViewModel.EndFrame == null || this.m_attributeViewModel.OptimizationPointCloudLayer == null)
      return false;
    return this.m_hasCableFrameAttributeChangedSinceLastCableUpdate || this.m_hasPointCloudAttributeChangedSinceLastCableUpdate || this.HasOptimizationDataChangedSinceLastCableUpdate() || this.m_attributeViewModel.HasParameterChanged();
  }

  private bool IsStatusBarMessageValid()
  {
    return this.m_attributeViewModel.StatusMessageType == CApCableStatusMessageType.None || this.m_attributeViewModel.StatusMessageType == CApCableStatusMessageType.Warning;
  }

  private bool IsRelevantProperty(string propertyName)
  {
    return propertyName == "IsLicenseOn" || propertyName == "CreationMode" || this.m_attributeViewModel.IsCreationParameter(propertyName) || this.m_attributeViewModel.IsFrameParameter(propertyName) || this.m_attributeViewModel.IsOptimizationParameter(propertyName);
  }

  private bool HasOptimizationDataChangedSinceLastCableUpdate()
  {
    ITxCableOptimizationData optimizationData = this.m_attributeViewModel.ComputeOptimizationData();
    bool flag = this.m_lastOptimizationData != null || optimizationData != null;
    if (flag)
    {
      flag = this.m_lastOptimizationData == null || optimizationData == null || this.m_lastOptimizationData.PointCloudLayer != optimizationData.PointCloudLayer;
      TxCableOptimizableParameter current;
      for (IEnumerator enumerator = Enum.GetValues(typeof (TxCableOptimizableParameter)).GetEnumerator(); !flag && enumerator.MoveNext(); flag = this.m_lastOptimizationData.ShouldOptimizeParameter(current) != optimizationData.ShouldOptimizeParameter(current))
        current = (TxCableOptimizableParameter) enumerator.Current;
    }
    return flag;
  }

  private void ResetObjectAttributeChangedTriggers()
  {
    this.m_hasCableFrameAttributeChangedSinceLastCableUpdate = false;
    this.m_hasPassNearFrameAttributeChangedSinceLastCableUpdate = false;
    this.m_hasPointCloudAttributeChangedSinceLastCableUpdate = false;
    this.m_lastOptimizationData = this.m_attributeViewModel.ComputeOptimizationData();
  }

  private void AttributeViewModel_PropertyChanging(object sender, PropertyChangedEventArgs e)
  {
    if (!this.IsRelevantProperty(e.PropertyName))
      return;
    this.UpdateGenerateButtonStatus();
  }

  private void AttributeViewModel_PropertyChanged(object sender, PropertyChangedEventArgs e)
  {
    bool flag = true;
    if (e.PropertyName == "Cable")
      this.ResetObjectAttributeChangedTriggers();
    else if (!this.IsRelevantProperty(e.PropertyName))
      flag = false;
    if (!flag)
      return;
    this.UpdateGenerateButtonStatus();
  }

  private void AttributeViewModel_PropertyAttributeChanged(
    object sender,
    PropertyChangedEventArgs e)
  {
    bool flag = true;
    switch (e.PropertyName)
    {
      case "Cable":
        this.ResetObjectAttributeChangedTriggers();
        break;
      case "StartFrame":
      case "EndFrame":
        this.m_hasCableFrameAttributeChangedSinceLastCableUpdate = true;
        break;
      case "PassNearFrame":
        this.m_hasPassNearFrameAttributeChangedSinceLastCableUpdate = true;
        break;
      case "OptimizationPointCloudLayer":
        this.m_hasPointCloudAttributeChangedSinceLastCableUpdate = true;
        break;
      default:
        flag = false;
        break;
    }
    if (!flag)
      return;
    this.UpdateGenerateButtonStatus();
  }
}
